
#include <iostream>
#include "Reactor.hpp"
#include "Connection.hpp"
#include "Listener.hpp"
#include "Parser.hpp"
#include "Calculator.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage : " << proc << " serverport" << std::endl;
}

// ./tcp_client serverport
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t serverport = std::stoi(argv[1]);

    // 日志服务
    EnableConsoleLogStrategy();

    // 业务对象
    std::unique_ptr<Calculator> cal = std::make_unique<Calculator>();

    // 协议的解析
    std::unique_ptr<Parser> par = std::make_unique<Parser>([&cal](Request &req) -> Response
                                                           { return cal->Exec(req); });

    // 连接管理器
    std::shared_ptr<Connection> listener = std::make_shared<Listener>(serverport);
    listener->SetCallBack([&par](std::string &inbuffer)->std::string{
        return par->Parse(inbuffer);
    });
    
    // Reactor容器
    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();

    // 添加连接管理
    R->AddConnection(listener);

    // 启动Reactor
    R->EventDispatcher();
    return 0;
}